<template>
<!-- 登陆模块 -->
  <div class="myprofile-container">

    <!-- 未登录模块 -->
    <div class="header not-login" v-if="!userTouken">
      <img src="~@/assets/mobile.png" alt="" />
      <span class="text" @click="$router.push('/login')">登陆 / 注册</span>
    </div>

    <!-- 已登录模块 -->
    <div class="header login" v-else>
      <div class="user-info">
        <div class="left">
          <van-image
            class="avatar"
            round
            fit="contain"
            :src="require('@/assets/shuangxiaba.jpg')"
          />
          <span class="user-name">{{userInfo.name}}</span>
        </div>
        <van-button type="default" round size="mini">编辑资料</van-button>
      </div>
      <div class="user-data">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>

    <!-- 宫格导航 -->
    <van-grid :column-num="2">
      <van-grid-item>
        <van-icon class="toutiao icon-tt-shoucang" slot="icon"/>
        <span slot="text" class="text">收 藏</span>
      </van-grid-item>
      <van-grid-item>
        <van-icon class="toutiao icon-tt-lishi" slot="icon"/>
        <span slot="text" class="text">历 史</span>
      </van-grid-item>
    </van-grid>

    <!-- cell单元格 -->
    <div class="cell-box">
      <van-cell title="消息通知" is-link />
      <van-cell title="小爱同学" is-link />
    </div>
      <van-cell title="退出登陆" @click="exit" class="exit" v-if="userTouken"/>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { getMyProfile } from '@/api/user'
export default {
  name: 'profile',
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['userTouken'])
  },
  created () {
    this.getMyInfo()
  },
  methods: {
    ...mapMutations(['getUserTouken']),
    // 退出登陆
    exit () {
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确认退出此账号?'
      })
        .then(() => {
          // this.$store.commit('getUserTouken', null)
          this.getUserTouken(null)
        })
        .catch(() => {})
    },
    // 获取个人信息
    async getMyInfo () {
      if (!this.userTouken) return
      try {
        const { data: res } = await getMyProfile()
        this.userInfo = res.data
      } catch (err) {
        this.$toast.fail('获取失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.myprofile-container {
  .header {
    width: 750px;
    height: 401px;
    background:url('~@/assets/banner.png') no-repeat center;
    background-size: cover;
  }
  // 未登录样式
  .not-login {
    display: flex;
    flex-direction:column;
    justify-content: center;
    align-items: center;
    font-size: 25px;
    color: #fff;
    .text {
      margin-top: 10px;
    }
    img {
      width: 132px;
    }
  }
  // 已登录样式
  .login {
    overflow: hidden;
    .user-info {
      height: 140px;
      margin-top: 100px;
      display: flex;
      justify-content:space-between;
      align-items: center;
      padding: 0 32px;
      .left {
        display: flex;
        align-items: center;
        vertical-align: middle;
        .avatar{
          width: 132px;
          height: 132px;
        }
        .user-name {
          font-size: 30px;
          color: #fff;
          margin-left: 22px;
        }
      }
    }
    .user-data {
      height: 161px;
      display:flex;
    }
    .data-item {
      flex: 1;
      display: flex;
      justify-content:center;
      align-items:center;
      flex-direction: column;
      color: #fff;
      span:nth-child(1) {
        font-size: 30px;
      }
      span:nth-child(2) {
        font-size: 23px;
      }
    }
  }
  // 宫格样式
  .toutiao {
    font-size: 45px
  }
  .icon-tt-shoucang {
    color: #EB5253;
  }
  .icon-tt-lishi {
    color: #FF9D1D;
  }
  .text {
    font-size: 28px;
    margin-top: 16px;
    letter-spacing: 10;
  }
  // cell样式
  .cell-box {
    margin: 10px 0;
  }
  .exit {
    text-align: center;
    color: #EB5253;
    font-weight: 700;
  }
}
</style>
